import { Component } from '@angular/core';
import { Student } from './model/student';
import { StudentService } from './service/student.service';
import { Studentvo } from './vo/studentvo';

@Component({
  selector: 'app-root',
  templateUrl: './app.component.html',
  styleUrls: ['./app.component.css']
})
export class AppComponent {
  title = 'app';

  //变量的定义、声明
  testvalue:string;
  numbervalue:number;

  student:Student;
  sl:Array<Student>;
  studentvo:Studentvo;


  constructor(private studentservice:StudentService){}

  //变量的初始化
ngOnInit() {
  // this.testvalue="文本框的显示";
  // this.numbervalue=66+128;

  // this.student.name="毛毛"
  // this.student.sex="女";
  // this.student.birthday=new Date().getTime();

  this.studentvo=new Studentvo();
  this.student=new Student();
  this.sl=new Array();
  this.loadStudentList();
  
  
  
}

//添加事件
add(){
  console.dir(this.studentvo);
  this.studentservice.addStudengInfo(this.studentvo)
  .then((data:any)=>{

  //   this.studentservice.getAnObject()
  // .then((data:any)=>{
  //   this.student=data;
  // })

  this.loadStudentList();
  })
}

//载入student列表
loadStudentList(){

  this.studentservice.getStudentList().
  then((data:any)=>{
    this.sl=data;
  })
}

//获得单个student对象
getToUpdateStudent(id:string){
  this.studentservice.getSingleStudent(id).
  then((data:Student)=>{
    this.studentvo=new Studentvo();
    this.studentvo.studentid=data.studentid;
    this.studentvo.xh=data.stunum;
    this.studentvo.xm=data.name;
  })

}

//删除student对象
deleteStudent(id:string){
  this.studentservice.deleteStudent(id).
  then((flag:boolean)=>{
    if(flag==true){
      this.loadStudentList();
    }
    else{
      alert("操作异常，请刷新后重试");
    }

  })
}

}

